﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using DevExpress.XtraBars;
using MINERP.COM;
using MINERP.MODEL.FoundationModel;

namespace MINERP.ComControls
{
    public partial class FrmParent : Form
    {
        public FrmParent()
        {
            InitializeComponent();
            //InitiAuthority();
        }
        public int userId { get; set; }
        public string userName { get; set; }
        public int GroupId { get; set; }
        public string GroupName { get; set; }
        public Dictionary<int, string> operates { get; set; }//<int TechId,string TechName>;
        public static List<string> ControlsName { get; set; }
        public string HeadPicPath { get; set; }
        public bool isShowMenuForm { get; set; }
        public int PrinterId { get; set; }
        public WorkShop_Info workShopInfo { get; set; }//车间信息

        /// <summary>
        /// 登录用户的节点id
        /// </summary>
        public int CurrentTechID { get; set; }

        /// <summary>
        /// 用户不可用控件的限制
        /// </summary>
        public void InitiAuthority()
        {
            foreach (Control control in this.Controls)
            {
                foreach (string controlName in ControlsName)
                {
                    if (control.Name == controlName)
                    {
                        control.Visible = false;
                    }
                }
            }

            Type t = this.GetType();

            foreach (var item in t.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField))
            {

                if (ControlsName.Contains(item.Name))
                {
                    object obj = item.GetValue(this);

                    if (obj.GetType() == typeof(ToolStripButton))
                    {
                        ToolStripButton it = obj as ToolStripButton;
                        if (it != null)
                        {
                            it.Visible = false;
                        }

                    }
                    else if (obj.GetType() == typeof(BarButtonItem))
                    {
                        BarButtonItem it = obj as BarButtonItem;
                        if (it != null)
                        {
                            it.Visibility = BarItemVisibility.Never;
                        }

                    }
                    else if (obj.GetType() == typeof(DevExpress.XtraNavBar.NavBarItem))
                    {
                        var it = obj as DevExpress.XtraNavBar.NavBarItem;
                        if (it != null)
                        {
                            it.Visible = false;
                        }
                        //((DevExpress.XtraNavBar.NavBarItem)obj).Visible = false;
                    }
                    //if (obj.GetType()==typeof(Control))
                    //{
                    else if (obj.GetType() == typeof(DevExpress.XtraTab.XtraTabPage))
                    {
                        var it = obj as DevExpress.XtraTab.XtraTabPage;
                        if (it != null)
                        {
                            it.PageVisible = false;
                        }
                    }
                    else
                    {
                        try
                        {
                            var it = obj as Control;
                            if (it != null)
                            {
                                it.Visible = false;
                            }
                            //((Control)obj).Visible = false;
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("无法对该控件进行限制" + ex.Message);
                        }
                    }
                    //}
                }
            }
        }

        public virtual void FrmParent_FormClosed(object sender, FormClosedEventArgs e)
        {
            Logoff.ShowMainFrm();
        }
    }

}
